#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int goal = 876543210;
int ops[][9] = {{3, 0, 1, 6, 4, 2, 7, 8, 5}, {0, 1, 2, 5, 3, 4, 6, 7, 8}};
int change(int x, int id) {
  int s[9] = {0};
  rep(i, 0, 8) s[i] = x % 10, x /= 10;
  int t[9] = {0};
  rep(i, 0, 8) t[i] = s[ops[id][i]];
  int res = 0;
  per(i, 8, 0) res = res * 10 + t[i];
  return res;
}

map<int, int> pre;
int astar(int s) {
  queue<int> q;
  map<int, int> dis;
  q.push(s);
  dis[s] = 0;
  pre[s] = 0;
  while (q.size()) {
    int s = q.front();
    q.pop();
    if (s == goal) return dis[s];
    rep(i, 0, 1) {
      int t = change(s, i);
      if (!dis.count(t)) {
        dis[t] = dis[s] + 1;
        q.push(t);
        pre[t] = s;
      }
    }
  }
  return -1;
}
void out(int x) {
  rep(i, 0, 8) {
    cout << x % 10 << " \n"[i % 3 == 2];
    x /= 10;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int x[9];
  int s = 0;
  rep(i, 0, 8) cin >> x[i];
  per(i, 8, 0) s = s * 10 + x[i];
  int d = astar(s);
  if (d == -1) {
    cout << "UNSOLVABLE";
    return 0;
  }

  vector<int> res;
  int t = goal;
  do {
    res.push_back(t);
    t = pre[t];
  } while (t);
  reverse(res.begin(), res.end());

  cout << d;
  for (int x : res) {
    cout << endl;
    out(x);
  }
  return 0;
}